package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.SkillItemsSet;
import com.xmy.cultivate.entity.SkillMeans;
import com.xmy.cultivate.entity.SkillShutnum;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISkillMeansService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Objects;

/**
 * <p>
 * 上传培训资料 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-06
 */
@RestController
@RequestMapping("/api/skillMeans")
public class SkillMeansController extends BaseController{

    @Autowired
    ISkillMeansService iSkillMeansService;
    @Autowired
    IQuarterService iQuarterService;

    @GetMapping("/getSkillMeans")
    public Result getSkillMeans(
                          @RequestParam(value = "itemsId", required = false) String itemsId,  //培训项目设置id
                          @RequestParam(value = "shutnumNum", required = false) String shutnumNum,
                          @RequestParam(value = "shutnumId", required = false) String shutnumId

    ){
        QueryWrapper<SkillMeans> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id",itemsId);
//        queryWrapper.eq("shutnum_num",shutnumNum);
        queryWrapper.eq("shutnum_id",shutnumId);
        queryWrapper.eq("deleted", 0);
        queryWrapper.last("limit 1");
        queryWrapper.orderByDesc("id");
        SkillMeans skillMeans = iSkillMeansService.getOne(queryWrapper);

        return new Result(Code.OK, skillMeans, "查询成功");
    }



    @PostMapping("/addSkillMeans")
    public Result addSkillMeans(@RequestBody SkillMeans skillMeans){
        Quarter nowQuarter = iQuarterService.getNowQuarter();
        Integer yearPart = nowQuarter.getYearPart();
        Integer quarterNum = nowQuarter.getNum();
        skillMeans.setYearPart(yearPart);
        skillMeans.setQuarterNum(quarterNum);
        skillMeans.setCreatedId(this.adminId);
        if (skillMeans.getDepartmentId()==null) {
            skillMeans.setDepartmentId(27L);
        } else {
            skillMeans.setDepartmentId(skillMeans.getDepartmentId());
        }
        boolean save = iSkillMeansService.save(skillMeans);
        if (!save){
            return new Result(Code.ERROR, null, "添加失败");
        }
        return new Result(Code.OK, null, "添加成功");
    }

    @PutMapping("/editSkillMeans")
    public Result editSkillMeans(@RequestBody SkillMeans skillMeans){
        SkillMeans skillMeansOld = iSkillMeansService.getById(skillMeans.getId());
        if (Objects.isNull(skillMeansOld)){
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        skillMeansOld.setContent(skillMeans.getContent());
        skillMeansOld.setFileLink(skillMeans.getFileLink());
        skillMeansOld.setFileName(skillMeans.getFileName());
        boolean update = iSkillMeansService.updateById(skillMeansOld);
        if (!update){
            return new Result(Code.ERROR, null, "修改失败");
        }
        return new Result(Code.OK, null, "修改成功");
    }


     @DeleteMapping("/deleteSkillMeans")
    public Result deleteSkillMeans(@RequestBody SkillMeans skillMeans) {
        SkillMeans skillMeansOld = iSkillMeansService.getById(skillMeans.getId());
        if (Objects.isNull(skillMeansOld)) {
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        boolean remove = iSkillMeansService.removeById(skillMeansOld);
        if (!remove) {
            return new Result(Code.ERROR, null, "删除失败");
        }
        return new Result(Code.OK, null, "删除成功");
    }

}
